home *** CD-ROM | disk | FTP | other *** search
- /*
- *--- PageMaker Utils.c --------------------------------------------------
- * Copyright (c) 1995-1996 Adobe Systems, Inc. All rights reserved.
- *
- * This collection of functions implements the routines to make callbacks
- * to PageMaker and routines to pack and unpack the buffers used in
- * callbacks. Most of these functions were formerly implemented as
- * macros, but have been converted into function for space efficiency
- * and compile time type-checking safety.
- *
- * Macintosh only - PageMaker does not preserve the current window/dialog
- * Port when responding to a command or query, so these callbacks do so by
- * using the GetPort()/SetPort() calls.
- *------------------------------------------------------------------------
- */
-
- #include "PMPlugin.h"
-
- #ifdef WINDOWS
- #include <Windows.h>
- #endif //WINDOWS
-
- #include "PMUtils.h"
-
- #define CallPageMaker(thePB) thePB->pmCallback(thePB)
-
- #ifdef WINDOWS
- PMHandle hDllInstance;
- #endif //WINDOWS
-
- static void ClearRequestBlock(sPMParamBlockPtr thePB);
- static void ClearReplyBlock(sPMParamBlockPtr thePB);
-
- #ifdef WINDOWS
- PMBool WINAPI DllMain(HINSTANCE hInstance, ULONG ul_reason, LPVOID lpvReserved);
- #endif
-
- //#pragma optimize("",off) // Plug-ins currently crash if the PBBin functions are optimized.
- /*
- *--- PBBinCommand -------------------------------------------------------
- * Issue a binary command to PageMaker. The binary command parameters
- * should be in the array pointed to by 'theRequest'. You should use
- * the LPPut<xxx> functions to build the binary command information.
- *------------------------------------------------------------------------
- */
- PMErr PBBinCommand(sPMParamBlockPtr thePB, ePMCommand op, ePMRefStyle theStyle, void * theRequest, unsigned long theRequestSize)
- {
- thePB->opCode = op;
- thePB->requestData = (PMHandle) theRequest;
- thePB->requestSize = theRequestSize;
- thePB->requestStyle = theStyle;
-
- ClearReplyBlock(thePB);
- return CallPageMaker(thePB);
- }
-
- /*
- *--- PBBinCommandByShortValue -------------------------------------------
- * Issue a command that requires a single short integer parameter (for
- * example, the Alignment command).
- *
- * The #ifdef statments are required because on the Macintosh platforms
- * a simple assignment of the short value to requestData puts the short value
- * in the wrong 16 bits - so the bitwise shift operator *<<* is used as the
- * remedy. On the Windows platforms a simple assignment is all that is needed.
- *------------------------------------------------------------------------
- */
- PMErr PBBinCommandByShortValue(sPMParamBlockPtr thePB, ePMCommand op, unsigned short v)
- {
- thePB->opCode = op;
- thePB->requestData = &v;
- thePB->requestSize = sizeof(short*);
- thePB->requestStyle = kXRSPointer;
-
- ClearReplyBlock(thePB);
- return CallPageMaker(thePB);
- }
- /*
- *--- PBBinCommandByLongValue --------------------------------------------
- * Issue a command that requires a single long integer parameter (for
- * example, the SelectID command).
- *------------------------------------------------------------------------
- */
- PMErr PBBinCommandByLongValue(sPMParamBlockPtr thePB, ePMCommand op, unsigned long v)
- {
- thePB->opCode = op;
- thePB->requestData = (void *)v;
- thePB->requestSize = sizeof(unsigned long);
- thePB->requestStyle = kXRSValue;
-
- ClearReplyBlock(thePB);
- return CallPageMaker(thePB);
-
- }
-
- /*
- *--- PBBinQuery ---------------------------------------------------------
- * Issue a binary query to PageMaker. The binary data will be returned
- * in the array 'r', which the caller must provide. You can use the
- * LPGet<xxx> functions to extract fields from the returned data.
- *
- * For queries that don't return string information, you can define
- * a C structure to directly access the returned information. See
- * StoryInfo.c for an example of this.
- *------------------------------------------------------------------------
- */
- PMErr PBBinQuery(sPMParamBlockPtr thePB, ePMQuery op, ePMRefStyle theStyle, void * r, unsigned long rsz)
- {
- thePB->opCode = op;
- ClearRequestBlock(thePB);
- thePB->replyStyle = theStyle;
- thePB->replyData = (PMHandle) r;
- thePB->replySize = rsz;
-
- return CallPageMaker(thePB);
- }
-
- /*
- *--- PBBinQueryWithParms ------------------------------------------------
- * Issue a binary query that requires parameters (for example,
- * GetColorInfo).
- *------------------------------------------------------------------------
- */
- PMErr PBBinQueryWithParms(sPMParamBlockPtr thePB, ePMQuery op,
- ePMRefStyle theRequestStyle, void * theRequestData, unsigned long theRequestSize,
- ePMRefStyle theReplyStyle, void * theReplyData, unsigned long theReplySize)
- {
- thePB->opCode = op;
-
- thePB->requestStyle = theRequestStyle;
- thePB->requestData = (PMHandle) theRequestData;
- thePB->requestSize = theRequestSize;
-
- thePB->replyStyle = theReplyStyle;
- thePB->replyData = (PMHandle) theReplyData;
- thePB->replySize = theReplySize;
-
- return CallPageMaker(thePB);
- }
-
- /*
- *--- PBTextCommand ------------------------------------------------------
- * Issue a text command to PageMaker.
- * Generally, you should use PBBinCommand, as binary commands are
- * more efficient.
- *------------------------------------------------------------------------
- */
-
- PMErr PBTextCommand(sPMParamBlockPtr thePB, ePMRefStyle theStyle, char * theText)
- {
- thePB->opCode = pm_execute_text;
- thePB->requestData = (PMHandle) theText;
- thePB->requestSize = strlen(theText) + 1;
- thePB->requestStyle = theStyle;
- ClearReplyBlock(thePB);
-
- return CallPageMaker(thePB);
- }
-
- /*
- *--- PBTextQuery --------------------------------------------------------
- * Issue a text query to PageMaker.
- * Generally you should use PBBinQuery, as binary queries are
- * more efficient.
- *------------------------------------------------------------------------
- */
-
- PMErr PBTextQuery(sPMParamBlockPtr thePB, char * theText, ePMRefStyle theStyle, char * theReplyBuf, unsigned long replyLen, ePMRefStyle theReplyStyle)
- {
- thePB->opCode = pm_evaluate_text;
- thePB->requestData = (PMHandle) theText;
- thePB->requestSize = strlen(theText) + 1;
- thePB->requestStyle = theStyle;
-
- thePB->replyData = (PMHandle) theReplyBuf;
- thePB->replySize = replyLen;
- thePB->replyStyle = theReplyStyle;
-
- return CallPageMaker(thePB);
- }
-
- //#pragma optimize("",on)
- /*
- *--- ClearRequestBlock/ClearReplyBlock ----------------------------------
- * Local functions to clear the sub-structures of the parameter block.
- *------------------------------------------------------------------------
- */
-
- void ClearRequestBlock(sPMParamBlockPtr thePB)
- {
- thePB->requestData = (PMHandle) NULL;
- thePB->requestSize = 0;
- thePB->requestStyle = kXRSNull;
- thePB->requestUnits = 0;
- }
-
- void ClearReplyBlock(sPMParamBlockPtr thePB)
- {
- thePB->replyData = (PMHandle) NULL;
- thePB->replySize = 0;
- thePB->replyStyle = kXRSNull;
- thePB->replyUnits = 0;
- }
-
- /*
- *--- LPGetString --------------------------------------------------------
- * Extracts a character string from the data reference given.
- *
- * pDst - pointer to destination string.
- * pSrc - pointer to source data block.
- * maxlen - max length of string to copy.
- *
- * return value is the number of copiable bytes plus any
- * null-terminating or pad bytes (pad to make an even byte boundary.)
- *------------------------------------------------------------------------
- */
- size_t LPGetString(char * pDst, const void * pSrc, short maxlen)
- {
- size_t tLen;
- tLen = strlen((char *)pSrc) + 1;
- if (tLen > maxlen)
- tLen = maxlen;
- memcpy(pDst, pSrc, tLen);
-
- if (tLen & 0x01)
- ++tLen;
- return tLen;
- }
-
- /*
- *--- LPPutString --------------------------------------------------------
- * Inserts the source string into the destination data block.
- *
- * pDst - pointer to destination data block.
- * pSrc - pointer to source string.
- * maxlen - max length of string to copy.
- *
- * return value is the number of copiable bytes plus any null-terminating
- * or pad bytes (pad to make an even byte boundary.)
- *------------------------------------------------------------------------
- */
- size_t LPPutString(void * pDst, const char * pSrc)
- {
- size_t tLen;
-
- tLen = strlen(pSrc) + 1;
- memcpy(pDst, pSrc, tLen);
-
- if (tLen & 0x01)
- ++tLen;
- return tLen;
- }
-
-
- /*
- *--- LPGetBuffer --------------------------------------------------------
- * Extracts a buffer from the data reference given.
- *
- * pDst - pointer to destination buffer.
- * pSrc - pointer to source data block.
- * len - number of bytes to transfer.
- *
- * return value is the number of bytes transfered plus
- * any pad bytes (pad to make an even byte boundary.)
- *------------------------------------------------------------------------
- */
- size_t LPGetBuffer(char * pDst, const void * pSrc, short len)
- {
- register size_t tLen = len;
- memcpy(pDst, pSrc, tLen);
-
- if (tLen & 0x01)
- ++tLen;
- return tLen;
- }
-
- /*
- *--- LPPutBuffer --------------------------------------------------------
- * Inserts the source buffer into the destination data block.
- *
- * pDst - pointer to destination data block.
- * pSrc - pointer to source buffer.
- * len - number of bytes to transfer.
- *
- * return value is the number of bytes transfered plus
- * any pad bytes (pad to make an even byte boundary.)
- *------------------------------------------------------------------------
- */
- size_t LPPutBuffer(void * pDst, const char * pSrc, short maxlen)
- {
- register size_t tLen = maxlen;
- memcpy(pDst, pSrc, tLen);
-
- if (tLen & 0x01)
- ++tLen;
- return tLen;
- }
-
- /*
- *--- LPPutShort/Long/Handle and LPGetShort/Long/Handle -----------------
- *These are functions that replace the macros by the same names.
- * You should use the functions rather than the macros because
- * they are not subject to unwanted side effects and are typesafe.
- *-----------------------------------------------------------------------
- */
- size_t LPPutShort(void * pDst, short pSrc)
- {
- *(short *) pDst = pSrc;
- return sizeof(short);
- }
-
- size_t LPPutLong(void * pDst, long pSrc)
- {
- *(long *) pDst = pSrc;
- return sizeof(long);
- }
-
- size_t LPPutULong(void * pDst, unsigned long pSrc)
- {
- *(unsigned long *) pDst = pSrc;
- return sizeof(unsigned long);
- }
-
- size_t LPPutHandle(void * pDst, PMHandle pSrc)
- {
- *(PMHandle *) pDst = pSrc;
- return sizeof(PMHandle);
- }
-
- size_t LPGetShort(short * pDst, const void * pSrc)
- {
- *pDst = * (short *) pSrc;
- return sizeof(short);
- }
-
- size_t LPGetLong(long * pDst, const void * pSrc)
- {
- *pDst = * (long *) pSrc;
- return sizeof(long);
- }
-
- size_t LPGetULong(unsigned long * pDst, const void * pSrc)
- {
- *pDst = * (unsigned long *) pSrc;
- return sizeof(unsigned long);
- }
-
- size_t LPGetHandle(PMHandle * pDst, const void * pSrc)
- {
- *pDst = * (PMHandle *) pSrc;
- return sizeof(PMHandle);
- }
-
-
- /*
- *--- BuildErrorMessages ---------------------------------------
- * This is a utility routine that takes a string parameter,
- * allocates a handle for it, and stuffs it in the
- * errMessage field of the parameter block.
- *
- * Note: The handle becomes property of PageMaker, which
- * will free it after it's displayed.
- *--------------------------------------------------------------
- */
- void BuildErrorMessages(sPMParamBlockPtr thePB, char * errText, char * cantText)
- {
- PMHandle h;
- char *ch;
- short lenErrText = strlen(errText);
- short lenCantText = strlen(cantText);
-
- //---------------------------------------------------------
- // PM requires that its strings be padded to even-byte
- // boundaries. This means that the length as reported by
- // strlen() must be odd so that when the NULL terminator
- // is added the length becomes even.
- //---------------------------------------------------------
- if (! (lenErrText & 0x01) ) ++lenErrText;
- if (! (lenCantText & 0x01) ) ++lenCantText;
-
- h = MMAlloc(lenErrText + lenCantText + 3); // 3 NULL terminators. Final one signifies end.
- ch = (char *) MMLock(h);
- ch += LPPutString(ch, errText);
-
- //---------------------------------------------------------
- // The "Can't" message is optional.
- // If it's present, PM does not display it's default
- // Cant message, so if we don't have one we must append
- // the final NULL to the end of the previous string.
- //---------------------------------------------------------
- if (*cantText)
- {
- ch += LPPutString(ch, cantText);
- }
-
- *ch = 0x00; // add the final NULL terminator.
-
- MMUnlock(h);
-
- thePB->errMessage = h;
- }
-
-
-
-
-